package com.estronger.shopmanage.ui.send.pg;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.tasking.DeliverDetailAdpter;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.DeliverDPG;
import com.estronger.shopmanage.bean.DeliverDetail;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.ConvertUtils;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.utils.ScreenUtils;
import com.estronger.shopmanage.utils.TimeUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.AppDialog;
import com.lidroid.xutils.http.RequestParams;

import cn.jesse.nativelogger.NLogger;

public class DeliverDetailActivity extends BaseActivity implements View.OnClickListener {
    private View rl_root;// 根布局view
    private Activity mContext;
    private TextView deliversname;//客户名
    private ImageView deliver_phone_call;//客户电话
    private ImageView deliver_send_sms;//客户短信
    private TextView deliver_item_location;//客户住址
    private TextView deliver_dgy;//导购员
    private ImageView deliver_phone_call2;//导购员电话
    private ImageView deliver_send_sms2;//导购员短信
    private TextView deliver_ckname;//仓库
    private TextView deliver_hpzs;//货品总数
    private TextView kdtime;//开单时间
    private ListView daisongdetail;//送货明细
    private DeliverDetailAdpter adpter;// 商品明细
    private FloatingActionButton fab;
    private Dialog dialog;
    private String _phone="13713713711";//客户的电话号码
    private String _phone2="";//d导购员的电话号码
    public String dlvid = "";
    public DeliverDetail data;
    public DeliverDPG.Datas.DeliverDPGListData singleData;

    // 初始化各个View
    @Override
    protected void initView() {
        try {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            rl_root = View.inflate(DeliverDetailActivity.this,
                    R.layout.activity_deliver_detail, null);
            setContentView(rl_root);
            mContext = DeliverDetailActivity.this;
            initview();
        }catch (Exception e){
            e.getMessage();
        }
    }

    private void initview(){
        deliversname = (TextView)rl_root.findViewById(R.id.deliversname);
        deliver_phone_call = (ImageView)rl_root.findViewById(R.id.deliver_phone_call);
        deliver_send_sms = (ImageView)rl_root.findViewById(R.id.deliver_send_sms);
        deliver_item_location = (TextView)rl_root.findViewById(R.id.deliver_item_location);
        deliver_dgy = (TextView)rl_root.findViewById(R.id.deliver_dgy);
        deliver_phone_call2 = (ImageView)rl_root.findViewById(R.id.deliver_phone_call2);
        deliver_send_sms2 = (ImageView)rl_root.findViewById(R.id.deliver_send_sms2);
        deliver_ckname = (TextView)rl_root.findViewById(R.id.deliver_ckname);
        deliver_hpzs = (TextView)rl_root.findViewById(R.id.deliver_hpzs);
        kdtime = (TextView)rl_root.findViewById(R.id.kdtime);
        daisongdetail = (ListView)rl_root.findViewById(R.id.daisongdetail);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(DeliverDetailActivity.this,DeliverPGTripActivity.class));
            };
        });
    }

    @Override
    protected void initAction() {
        deliver_phone_call.setOnClickListener(this);
        deliver_send_sms.setOnClickListener(this);
        deliver_phone_call2.setOnClickListener(this);
        deliver_send_sms2.setOnClickListener(this);
        daisongdetail.setFocusable(false);

        dlvid = getIntent().getStringExtra("dlvid");
        singleData = (DeliverDPG.Datas.DeliverDPGListData)getIntent().getSerializableExtra("singleData");
        if(dlvid == null){
            dlvid = "";
        }

        //请求数据
        getGoodsHead();
        getGoodsList();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.deliver_phone_call:
                if (isRightNumber(_phone)) {
                    dialog = AppDialog.createDialog(mContext,
                            "["+deliversname.getText()+"]", _phone, this, "");
                    dialog.show();
                }
                break;
            case R.id.deliver_send_sms:
                if (isRightNumber(_phone)) {
                    Uri uri = Uri.parse("smsto:" + _phone);
                    intent = new Intent(Intent.ACTION_SENDTO, uri);
                    startActivity(intent);
                }
                break;
            case R.id.deliver_phone_call2:
                if (isRightNumber(_phone2)) {
                    dialog = AppDialog.createDialog(mContext,
                            "["+deliversname.getText()+"]", _phone2, this, "");
                    dialog.show();
                }
                break;
            case R.id.deliver_send_sms2:
                if (isRightNumber(_phone2)) {
                    Uri uri = Uri.parse("smsto:" + _phone2);
                    intent = new Intent(Intent.ACTION_SENDTO, uri);
                    startActivity(intent);
                }
                break;
            case R.id.ok:
                try {
                    if(PermissionUtils.isCallPhoneGranted(mContext)) {
                        intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                                + _phone));
                        mContext.startActivity(intent);
                        dialog.dismiss();
                    }else{
                        PermissionUtils.ToSetPermissions(mContext,R.string.PermissionsCallPhone);
                    }
                }catch (Exception e){
                    NLogger.e(""+e.getMessage());
                }
                break;
        }
    }

    //加载头
    public void getGoodsHead(){
        if(singleData!=null){
            deliversname.setText(ConvertUtils.formatNullF(singleData.buildname) + ConvertUtils.formatNull(singleData.cstname));
            deliver_item_location.setText(ConvertUtils.formatNull(singleData.address) + "");
            deliver_ckname.setText(ConvertUtils.formatNull(singleData.stkname) + "");
            deliver_hpzs.setText(ConvertUtils.formatNull(singleData.dlvqty) + "");
            kdtime.setText(TimeUtils.cutsecond(singleData.cdate));
            _phone = singleData.cstmobile;
        }
    }

    // 获取送货商品列表
    public void getGoodsList() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("sf_action", "getdlvgoodslist");
        params.addBodyParameter("usercode", MyApplication.getUserCode());
        params.addBodyParameter("dlvid", dlvid);
        netManager.connectWork(params, NetManager.DELIVER_GOODSLIST);
    }

    protected NetManager netManager = new NetManager() {
        @Override
        public void onNetWorkErorr(int code, String result) {
            Toast.show(mContext,"请求失败:" + result,2000);
        }

        @Override
        public void onNetWorkSuccess(int code, String result) {
            if (code == NetManager.DELIVER_GOODSLIST) {
                data = gson.fromJson(result, DeliverDetail.class);
                if (data.success) {
                    adpter = new DeliverDetailAdpter(mContext, data);
                    daisongdetail.setAdapter(adpter);
                    daisongdetail.setDivider(null);

                    //重算高度
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.dpToPx(mContext, 135) * data.data.size());
                    daisongdetail.setLayoutParams(params);
                }
            }
        }
    };

//    private void initdata(){
//        data = new ArrayList<DeliverDetail>();
//        for(int i=0;i<5;i++){
//            DeliverDetail dd = new DeliverDetail();
//            dd.setSpname("美式蜘蛛网龟背扶手椅");
//            dd.setSpguige("长636mm*宽618mm*高930mm");
//            dd.setSpshuliang("2");
//            dd.setSppic("http://shit.com");
//            data.add(dd);
//        }
//
//        adpter = new DeliverDetailAdpter(mContext,data);
//        daisongdetail.setAdapter(adpter);
//        daisongdetail.setDivider(null);
//
//        //重算高度
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.dpToPx(mContext,135) * data.size());
//        daisongdetail.setLayoutParams(params);
//
//        //
//        deliver_hpzs.setText(data.size()+"");
//    }
}
